<?php

use yii\helpers\Url;

$web = Url::base();

?>

<script type="application/javascript">

    function getAreaOther(parent_id) {
        var data = {};
        data.parent_id = parent_id;
        var re = requestJson("get-area-handler", data);
        if (re.status == 0) {
            return re.data.others;
        } else {
            alert(re.message);
        }
    }


    $(function ($) {
        var vitem = <?=json_encode($item)?>

        var sheng_lable = "省";
        var shi_lable = "市";
        var qu_lable = "区";
        var default_sheng = '<option value="">{0}</option>'._format(sheng_lable);
        var default_shi = '<option value="">{0}</option>'._format(shi_lable);
        var default_qu = '<option value="">{0}</option>'._format(qu_lable);
        $("#province_name").html(default_sheng);//sel_sheng  province_name
        $("#city_name").html(default_shi);//sel_shi  city_name
        $("#region_name").html(default_qu);//sel_qu  region_name
        var shengs = getAreaOther(null);

        if (shengs != false) {
            $.each(shengs, function (i, item) {
                $("#province_name").append("<option  value='" + item.id + "'>" + item.name + "</option>");
            });
            $("#province_name").val(null);
        }

        //如果是修改数据则需填充省市区并选择默认值
        if (vitem != null && vitem != undefined) {
            var province_id = "";
            var city_id = "";
            var province_count=$("#province_name option").length;
            for(var i=0;i<province_count;i++)
            {
                if($("#province_name").get(0).options[i].text == vitem['province_name'])
                {
                    $("#province_name").get(0).options[i].selected = true;
                    province_id = $("#province_name").get(0).options[i].value;
                    break;
                }
            }
            //填充市
            if(province_id!=null && province_id!=''){
                var shis = getAreaOther(province_id);
                if (shis != false) {
                    $.each(shis, function (i, item) {
                        $("#city_name").append("<option value='" + item.id + "'>" + item.name + "</option>");
                    });
                    $("#city_name").val(null);
                }
                var city_count=$("#city_name option").length;
                for(var i=0;i<city_count;i++)
                {
                    if($("#city_name").get(0).options[i].text == vitem['city_name'])
                    {
                        $("#city_name").get(0).options[i].selected = true;
                        city_id = $("#city_name").get(0).options[i].value;
                        break;
                    }
                }
            }
            //填充区
            if(city_id!=null && city_id!=''){
                var qu = getAreaOther(city_id);
                if (qu != false) {
                    $.each(qu, function (i, item) {
                        $("#region_name").append("<option value='" + item.id + "'>" + item.name + "</option>");
                    });
                    $("#region_name").val(null);
                }
                var qu_count=$("#region_name option").length;
                for(var i=0;i<qu_count;i++)
                {
                    if($("#region_name").get(0).options[i].text == vitem['region_name'])
                    {
                        $("#region_name").get(0).options[i].selected = true;
                        break;
                    }
                }
            }
        }

        $("#province_name").change(function () {
            var me = $(this);
            $("#city_name").html(default_shi);
            $("#region_name").html(default_qu);
            var shis = getAreaOther(me.val());
            if (shis != false) {
                $.each(shis, function (i, item) {
                    $("#city_name").append("<option value='" + item.id + "'>" + item.name + "</option>");
                });
                $("#city_name").val(null);
            }
        });

        $("#city_name").change(function () {
            var me = $(this);
            $("#region_name").html(default_qu);
            var qu = getAreaOther(me.val());
            if (qu != false) {
                $.each(qu, function (i, item) {
                    $("#region_name").append("<option value='" + item.id + "'>" + item.name + "</option>");
                });
                $("#region_name").val(null);
            }
        });

    });

</script>
<?php
if ($item != null) {
    ?>
    <div>
        <input name="address_id" id="address_id" type="hidden" value="<?= $item['id'] ?>"/>
        <p class="name">收货姓名：<input type="text" name="name" id="name" value="<?= $item['name'] ?>"/><span>只包含中文汉字，最多4个字</span></p>
        <p class="phone">手机号码：<input type="text" name="phone" id="phone" value="<?= $item['phone'] ?>"/></p>
        <div class="info">
            <p class="city fl">选择省市：</p>
            <div class="fl">
                <select name="province_name" id="province_name" style="max-width: 100px"><option value="1"><?= $item['province_name'] ?></option></select>
                <select name="city_name" id="city_name" style="max-width: 100px"><option value="1"><?= $item['city_name'] ?></option></select>
                <select name="region_name" id="region_name" style="max-width: 135px"><option value="1"><?= $item['region_name'] ?></option></select>
            </div>
            <div class="clfl"></div>
        </div>
        <p class="row">详细街道：<input type="text" name="address" id="address" value="<?= $item['address'] ?>"/></p>
        <p class="remark">备注：<input type="text" name="remark" id="remark" value="<?= $item['remark'] ?>"/></p>
        <input type="submit" onclick="save()" value="确认提交"/>
    </div>
    <?php
} else {
    ?>
    <div>
        <input name="address_id" id="address_id" type="hidden" value=""/>
        <p class="name">收货姓名：<input type="text" name="name" id="name" value=""/><span>只包含中文汉字，最多4个字</span></p>
        <p class="phone">手机号码：<input type="text" name="phone" id="phone" value=""/></p>
        <div class="info">
            <p class="city fl">选择省市：</p>
            <div class="fl">
                <select name="province_name" id="province_name" style="max-width: 100px"><option value="1">省</option></select>
                <select name="city_name" id="city_name" style="max-width: 100px"><option value="1">市</option></select>
                <select name="region_name" id="region_name" style="max-width: 135px"><option value="1">区</option></select>
            </div>
            <div class="clfl"></div>
        </div>
        <p class="row">详细街道：<input type="text" name="address" id="address" value=""/></p>
        <p class="remark">备注：<input type="text" name="remark" id="remark" value=""/></p>
        <input type="submit" onclick="save()" value="确认提交"/>

    </div>
    <?php
}
?>

